package util.android;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
El equipo de desarrollo de Android recomienda usar XmlPullParser para parsear un XML 
porque es mas eficiente que el parser DOM (DocumentBuilder)
http://developer.android.com/training/basics/network-ops/xml.html 
 */

import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import util.patron.command.Command2;

public class XmlAndroidUtil {

	public static void parsear(InputStream is, Command2<Integer,XmlPullParser> operador) throws XmlPullParserException, IOException {
		XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
       
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        parser = factory.newPullParser();
 
        parser.setInput(is, null);
 
        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
        	try {
				operador.execute(eventType,parser);
			} catch (Exception e) {
				throw new XmlPullParserException("",parser,e);
			}
            eventType = parser.next();
        }
        
	}
	
	public static XmlPullParser getParser(File f) throws FileNotFoundException, XmlPullParserException {
		InputStream is = new FileInputStream(f);
		
		XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
		factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        parser = factory.newPullParser(); 
        parser.setInput(is, null);
        return parser;
	}

}
